package com.money.demo.thread;

import com.money.activemq.MQConsumer;
import com.money.dao.MoneyDetailMapper;
import com.money.dao.PersonMapper;
import com.money.demo.common.Constants;
import com.money.demo.object.MoneyBag;
import com.money.domain.MoneyDetail;
import com.money.domain.Person;
import org.apache.activemq.command.ActiveMQObjectMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import java.util.Date;


public class Consumer implements Runnable {

    private Person person;

    private MoneyDetailMapper moneyDetailMapper;
    private PersonMapper personMapper;

//    public Consumer(Person person) {
//        this.person = person;
//    }

    public Consumer(Person person, MoneyDetailMapper moneyDetailMapper, PersonMapper personMapper) {
        this.person = person;
        this.moneyDetailMapper = moneyDetailMapper;
        this.personMapper = personMapper;
    }

    @Override
    public void run() {

        while (true){

            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            try {
                MessageConsumer consumer=MQConsumer.consume(person.getGroupId()+"");
                //7. 创建一个监听器
                consumer.setMessageListener(message -> {
                    try {
                        if(message==null || ((ActiveMQObjectMessage) message).getObject()==null){
                            throw new RuntimeException(person.getGroupName()+"："+person.getName()+"的红包已抢完");
                        }


                        MoneyBag bag= (MoneyBag) ((ActiveMQObjectMessage) message).getObject();

                        if(bag.getGroup().intValue()!=person.getGroupId().intValue()){
                            return;
                        }

                        long diff=(new Date().getTime()-bag.getTime().getTime())/1000;
                        if(diff> Constants.MONEY_EXPIRE_SECONDES){
                            throw new RuntimeException(person.getGroupName()+"："+person.getName()+"的红包已过期");
                        }

                        moneyDetailMapper.selectByPrimaryKey(11l);


                        MoneyDetail detail=new MoneyDetail();
                        detail.setMoneyId(bag.getMoneyId());
                        detail.setId(System.currentTimeMillis());
                        detail.setSender(bag.getSender());
                        detail.setSenderName(bag.getSenderName());
                        detail.setSenderGroup(bag.getGroup());
                        detail.setSenderGroupName(bag.getGroupName());
                        detail.setReceiver(person.getId());
                        detail.setReceiverName(person.getName());
                        detail.setReceiverGroup(person.getGroupId());
                        detail.setReceiverGroupName(person.getGroupName());
                        detail.setMoney(bag.getMoney());
                        detail.setTime(new Date());
                        moneyDetailMapper.insert(detail);

                        person.setMoney((person.getMoney()==null ? 0 : person.getMoney())+bag.getMoney());
                        personMapper.updateByPrimaryKeySelective(person);


                        System.out.println("用户"+person.getId()+"抢了"+bag.getSender()+"的红包"+bag.getMoney()+"元");


                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });

            } catch (JMSException e) {
                e.printStackTrace();
            }

        }




    }

}
